package sss.lru;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: java-interview
 * @description: lru算法
 * @author: DingZhenYun
 * @create: 2021-11-11 19:33
 **/

public class LRUCacheDemo1 {
    // map负责查找，构建一个虚拟的双向链表，它里面安装的就是一个Node节点，作为数据载体
    // 1.构造一个Node节点作为数据载体
    class Node<K,V>{
        K key;
        V value;
        Node<K,V> prev;
        Node<K,V> next;
        public Node(){
            this.next=this.prev=null;
        }
        public Node(K key,V value){
            this.key=key;
            this.value=value;
            this.next=this.prev=null;

        }
    }

    // 2.构造一个双向链表，用于存放Node节点
    class DoubleLinkedList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;


        public DoubleLinkedList(){
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }
        // 添加节点
        public void addHead(Node<K, V> node) {
            // 当前节点后继指针指向头节点后继节点（尾节点）
            node.next = head.next;
            // 尾节点前驱指针指向当前节点
            head.next.prev = node;
            // 头节点后继指针指向当前节点
            head.next = node;
            // 当前节点前驱指针指向头节点
            node.prev = head;
        }

        // 删除节点
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            // 当前节点的指针全部指向空，相当于出队
            node.prev = null;
            node.next = null;
        }

        // 获得最后一个节点
        public Node getLast() {
            return tail.prev;
        }
    }

    // 容量大小
    private int cacheSize;
    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LRUCacheDemo1(int cacheSize) {
        this.cacheSize= cacheSize;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }


    public int get(int key) {
        if (!map.containsKey(key)) {
            return  -1;
        }

        Node<Integer, Integer> node = map.get(key);
        // 频繁使用（读和写）的现从链表中删除，再加到队头
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    // 更新和新增
    public void put(int key, int value) {
        // 更新节点
        if (map.containsKey(key)) {
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);

            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            // 容量达到最大值
            if (map.size() == cacheSize) {
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            // 新增节点
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);
        }
    }
    public static void main (String[]args){
        LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);

        lruCacheDemo.put(1, "a");
        lruCacheDemo.put(2, "b");
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());

        lruCacheDemo.put(4, "d");
        System.out.println(lruCacheDemo.keySet());

        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(5, "x");
        System.out.println(lruCacheDemo.keySet());
    }
}
